var chatwebsocket
var jsonmsg ={"type":"","username":"","roomid":"","msg":""}


function chat(username,roomid) {
    



   var url ="wss://119.23.252.15:8443/chat";


    chatwebsocket = new WebSocket(url);


    chatwebsocket.onerror = function () {
        alert('出现错误')

    };

    //连接成功建立的回调方法
    chatwebsocket.onopen = function (event) {
        //alert('连接成功')
        jsonmsg.type="log";
        jsonmsg.username = username
        jsonmsg.roomid = roomid    
        
        chatwebsocket.send("{type:'log',username:'danwich',roomid:'11'}")
        
   
         //chatwebsocket.send(JSON.stringify(jsonmsg))
        //alert("初始化");
        
       // alert(JSON.stringify(jsonmsg))
    
    }
    console.log("-----")
    //接收到消息的回调方法
    chatwebsocket.onmessage = function (msg) {
        document.getElementById('chat').innerHTML += '\n'+ msg.data
    }

    //连接关闭的回调方法
    chatwebsocket.onclose = function () {
        alert('关闭')

    }


        
    

}

function Send2Server(){
    //alert("发送")
    var msg =document.getElementById("ipmsg").value
    jsonmsg.type = "chat"
    jsonmsg.msg = msg
    //alert(JSON.stringify(jsonmsg))
    chatwebsocket.send(JSON.stringify(jsonmsg))
    document.getElementById("ipmsg").value = ''
}